package routes

import (
	"net/http"

	"github.com/gorilla/mux"
)

// GetQueryParam retrieves the value of a query parameter by name.
//
// If the parameter is not present, it returns the provided default value or an empty string.
func GetQueryParam(r *http.Request, name string, defaultValue ...string) string {
	v := r.URL.Query().Get(name)
	if v != "" {
		return v
	}

	if len(defaultValue) > 0 {
		return defaultValue[0]
	}

	return ""
}

// GetPathVar retrieves the value of a path variable by name.
//
// If the variable is not present, it returns the provided default value or an empty string.
func GetPathVar(r *http.Request, name string, defaultValue ...string) string {
	v := mux.Vars(r)[name]
	if v != "" {
		return v
	}

	if len(defaultValue) > 0 {
		return defaultValue[0]
	}

	return ""
}
